package Jogo.States.Phases {
	import Jogo.Comm.MsgHandler;
	import Jogo.Entities.Enemy;
	import Jogo.Entities.Tile;
	import Jogo.Entities.Turret;
	import Jogo.Entities.TurretPoint;
	import Jogo.Main;
	import Jogo.States.PhaseState;
	import Jogo.Util;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	
	public class Phase1 extends PhaseState {
		/* map */
		[Embed(source = "../../../Img/Phase1/Fase1.png")]
		private static var mapBMP:Class;
		private static var map : Bitmap;
		
		/* The text file that describes this phase */
		private const FILE_NAME : String = "phase1.txt";	
		
		/* The width and the height of the map */
		private const MAP_WIDTH : Number = 768;
		private const MAP_HEIGHT : Number = 768;
		
		/* Where the map tile set really begins.
		* excluding HUD elements, edges, etc...
		*/
		public static const TILE_SET_BEGIN_POINT 	: Point = new Point(256,0);
		public static const TILE_SIDE			 	: Number = 77;
		
		/* Number of lines (y) and number of rows (x) */
		private static const MATRIX_SIZE : Point = new Point(10,10);
		
		public function Phase1() {
			tileSide = TILE_SIDE;
			
			super();
			map = new mapBMP();
			map.width = MAP_WIDTH;
			map.height = MAP_HEIGHT;
			map.x = TILE_SET_BEGIN_POINT.x;
			map.y = TILE_SET_BEGIN_POINT.y;
			
			
			/* Add things to stage in the right order */
			root.addChild(map);
			//sidePanelRoot.addChild(playersTF);
			/*root.addChild(tf);
			root.addChild(tfMap);
			root.addChild(currentPlayer);
			root.addChild(moneyTF);*/
			//root.addChild(playersTF);
			
			TOTAL_WAVES = 5;
			hp = 20;
			
			createPhase();
			sortTurretPoints();
			currentWave++;
			wave = createWave(currentWave);
			super.callNextWave(wave);
		}
		
		override public function enterFrame(e : Event) : void{
			
			super.enterFrame(e);

			if ((enemies.length == 0) && (wave.length == 0)){
				if (currentWave <= TOTAL_WAVES) {
					currentWave++;
					wave = createWave(currentWave);
					super.callNextWave(wave);
				}
			}
		}
		
		private function createWave(waveId : int) : Vector.<Enemy>{//Create wave tem que passar o nto da fase tbm
			var waveT : Vector.<Enemy> = new Vector.<Enemy>;			
			
			/* for each of the TOTAL_WAVES waves, push the enemies
			* that are on that wave
			*/
			/*switch (waveId){
				case 1:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					break;
				case 2:
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					break;
				case 3:
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					break;
				case 4:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 3));
					break;
				case 5:
					waveT.push(new Enemy(tiles[6][0], 0, 3));
					waveT.push(new Enemy(tiles[6][0], 0, 3));
					waveT.push(new Enemy(tiles[6][0], 0, 3));
				break;
			}*/
			
			switch (waveId){
				case 1:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					break;
				case 2:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					break;
				case 3:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					break;
				case 4:
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[2][0], 0, 0));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 1));
					waveT.push(new Enemy(tiles[6][0], 0, 1));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 2));
					waveT.push(new Enemy(tiles[6][0], 0, 2));
					waveT.push(new Enemy(tiles[2][0], 0, 3));
					break;
				case 5:
					waveT.push(new Enemy(tiles[6][0], 0, 3));
					waveT.push(new Enemy(tiles[6][0], 0, 3));
					waveT.push(new Enemy(tiles[6][0], 0, 3));
					break;
			}
			
			return waveT;

			
		}
		
		
		
		
		
		
		
		private static function createPhase() : void{
			createPhaseTiles();
			var points : Vector.<Point> = new Vector.<Point>();
			
			points.push(new Point(2,2));
			points.push(new Point(5,2));
			points.push(new Point(8,2));
			points.push(new Point(1,4));
			points.push(new Point(3,5));
			points.push(new Point(7,6));
			points.push(new Point(2,7));
			points.push(new Point(4,8));
			points.push(new Point(8,8));
			
			createTurretPoints(points);
		}
		
		
		private static function createTurretPoints(tpa : Vector.<Point>) : void{
			var tp : TurretPoint;
			while (tpa.length > 0){
				
				tp = new TurretPoint(tpa.shift());
				turretPoints.push(tp);
				root.addChild(tp.getGraphicsRoot());
			}
			
		}
		
		
		private static function createPhaseTiles() : void{
			var i : int;
			var j : int;
			var pt : Point;
			
			//Create the tiles
			tiles = new Vector.<Vector.<Tile>>(MATRIX_SIZE.y);
			for(i = 0; i < MATRIX_SIZE.y; i++) {
				tiles[i] = new Vector.<Tile>(MATRIX_SIZE.x);
				for (j=0; j < MATRIX_SIZE.x; j++){
					pt = new Point();
					pt.x = TILE_SET_BEGIN_POINT.x + TILE_SIDE/2 + j*TILE_SIDE;
					pt.y = TILE_SET_BEGIN_POINT.y + TILE_SIDE/2 + i*TILE_SIDE;
					tiles[i][j] = new Tile(pt);
				}
			}
			
			/* Populates the tiles with the paths */
			var str : String = new String();
			str =  "0,3,1,2,1,4\n0,9,1,8\n1,1,2,0,1,2\n1,2,1,1,0,3\n1,4,0,3,2,4,1,5\n" +
				"1,5,1,4,1,6\n1,6,1,5,1,7\n1,7,1,6,2,6,1,8\n1,8,1,7,0,9\n2,0,1,1\n" +
				"2,4,1,4,3,4\n2,6,1,7,3,7\n3,2,4,2,3,3\n3,3,3,2,4,4\n3,4,2,4,4,4,3,5\n" +
				"3,5,3,4,4,6\n3,7,2,6,3,8,4,7\n3,8,3,7,4,8\n4,2,5,1,3,2\n4,4,3,3,3,4,5,4\n" +
				"4,6,3,5,4,7\n4,7,4,6,3,7\n4,8,3,8,5,8\n5,1,4,2,6,1\n5,4,4,4,6,5\n5,7,6,6,5,8\n" +
				"5,8,4,8,5,9,5,7\n5,9,5,8\n6,0,6,1\n6,1,6,0,5,1,7,1\n6,5,5,4,6,6,7,5\n" +
				"6,6,6,5,5,7,7,7\n7,1,6,1,8,1\n7,5,6,5,8,5\n7,7,6,6,7,8,8,7\n7,8,7,7,7,9\n" +
				"7,9,7,8\n8,1,7,1,8,2\n8,2,8,1,8,3\n8,3,8,2,9,4\n8,5,7,5,9,5,8,6\n" +
				"8,6,8,5,8,7\n8,7,8,6,7,7\n9,4,8,3,9,5\n9,5,9,4,8,5\n";
			var lineA : Array = str.split("\n");
			for(var a : int = 0; a<lineA.length; a++){
				var lineS : String = lineA[a].toString();
				var nbr : Array = lineS.split(",");
				
				var start : Point = new Point(nbr[0], nbr[1]);
				for(var b : int = 2; b<nbr.length; b=b+2){
					var pta : Point = new Point(nbr[b], nbr[b+1]);
					tiles[start.x][start.y].pathsPush(tiles[pta.x][pta.y]);
					tiles[start.x][start.y].setType(Tile.ENEMY_WAY);
				}
			}
			
			//(X,Y) = (Y,X). Tá trocado >.<
			tiles[2][0].setType(Tile.START_POINT);
			tiles[6][0].setType(Tile.START_POINT);
			
			tiles[0][9].setType(Tile.END_POINT);
			tiles[5][9].setType(Tile.END_POINT);
			tiles[7][9].setType(Tile.END_POINT);
		}
	}
}